package data.join.com.application;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;

import com.umeng.analytics.MobclickAgent;

import java.util.List;

import data.join.com.database.GreenDao;
import data.join.com.http.HttpServer;
import data.join.com.media.AppUpdateThread;
import data.join.com.media.VideoScannerThread;
import data.join.com.service.JobSchedulerService;
import data.join.com.service.MainService;
import data.join.com.utils.ImageUtils;
import data.join.com.utils.LogcatHelper;
import data.join.com.utils.MyLog;
import data.join.com.utils.QuickShPref;
import data.join.com.utils.WakeLockUtils;

/**
 * Created by Administrator on 2017/9/18.
 */

public class RecordApp extends Application {

    private static RecordApp recordApp;

    @Override
    public void onCreate() {
        super.onCreate();

        recordApp = this;

        appInit();
    }

    private void appInit(){
        MyLog.D("RecordApp appInit start");

        QuickShPref.init(this);
        LogcatHelper.getInstance(this).start();

        getStartLock();

        MobclickAgent.onResume(this);
        GreenDao.init(this);
        ImageUtils.init(this);

        MainService.startMainService(this);
        startService(new Intent(this, JobSchedulerService.class));

        MyLog.D("RecordApp appInit end");
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        MobclickAgent.onKillProcess(this);
    }

    public static Context getDefault(){
        return recordApp;
    }

    private void getStartLock(){
        long last = QuickShPref.getInstance().getLong(QuickShPref.LastStartLock);

        if(System.currentTimeMillis() - last < 60*1000){
            MyLog.D("getStartLock delta time less 30s");
        }else{
            WakeLockUtils.getDefault(this).acquireWakeLock(AppUpdateThread.INSTALL_LOCK_TAG,60*1000);
        }
        QuickShPref.getInstance().putValueObject(QuickShPref.LastStartLock,System.currentTimeMillis());
    }
}
